//
// Created by 寻欢 on 2021-10-14.
//

//2019240004 董博阳 大数据191 实验2 进程管理

/*
 * 原因是没有采取线程同步
 */
#include <windows.h>
#include <process.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int number = 1;

unsigned __stdcall ThreadProc1(void* pArguments) {
    while (number < 10) {
        printf(" sub thread 1 : number = %d\n", number);
        if(number < 10) ++number;
    }
    _endthreadex( 0 );
    return 0;
}
unsigned __stdcall ThreadProc2(void* pArguments) {
    while (number < 10) {
        printf(" sub thread 2 : number = %d\n", number);
        if(number < 10) ++number;
    }
    _endthreadex( 0 );
    return 0;
}

int main() {
    HANDLE hHandles[2];
    unsigned threadID[2];

    hHandles[0] = (HANDLE)_beginthreadex(NULL, 0, &ThreadProc1, NULL, 0,
                                         &threadID[0]);
    hHandles[1] = (HANDLE)_beginthreadex(NULL, 0, &ThreadProc2, NULL, 0,
                                         &threadID[1]);

    WaitForMultipleObjects( 2, hHandles, TRUE, INFINITE );
    printf( " main thread  : number = %d\n", number );


    CloseHandle( hHandles[0] );
    CloseHandle( hHandles[1] );
    getch();
    return 0;
}